package com.standardoperationapp.utils;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.os.Build;

public class AlarmUtil {
    // 设置一次性报警
    public static void setOneTimeAlarm(Context context, Intent intent) {
        // 设置报警的时间
        long triggerAtMillis = System.currentTimeMillis() + 5 * 60 * 1000; // 5分钟后报警
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager am = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        am.setExact(AlarmManager.RTC_WAKEUP, triggerAtMillis, pendingIntent);
    }

    // 取消已设置的报警
    public static void cancelAlarm(Context context, Intent intent) {
        PendingIntent pendingIntent = PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
        AlarmManager alarmManager = (AlarmManager) context.getSystemService(Context.ALARM_SERVICE);
        if (alarmManager != null) {
            alarmManager.cancel(pendingIntent);
        }
    }
}
